﻿using System.IO;
using System.Xml.Serialization;
using System.Text;

namespace MyFloridaHouse.libSvUtilities
{
    public static class ConfigManager
    {
        public static ConfigItem ConfigFromXML(string strXML)
        {
            byte[] byteArray = Encoding.UTF8.GetBytes(strXML);
            return ConfigFromXML(byteArray);
        }

        public static ConfigItem ConfigFromXML(byte[] byteXML)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(ConfigItem));
            MemoryStream stream = new MemoryStream(byteXML);
            ConfigItem config;
            config = (ConfigItem)deserializer.Deserialize(stream);
            return config;
        }

        static public string ConfigToXML(ConfigItem configTree)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ConfigItem));
            MemoryStream memstream = new MemoryStream();
            serializer.Serialize(memstream, configTree);
            using (var ms = new MemoryStream())
            {
                memstream.Position = 0;
                var sr = new StreamReader(memstream);
                return sr.ReadToEnd();
            } 
        }

    }
}

